<template>
  <ContentWrap>
    <el-form :model="queryParams" ref="queryFormRef" label-width="auto" :inline="true">
      <el-form-item label="服务器ip" prop="ip">
        <el-input v-model="queryParams.ip" placeholder="请输入服务器ip" :clearable="true" />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleClickSearch">
          <Icon icon="ep:search" />
          搜索
        </el-button>
        <el-button @click="handleClickReset">
          <Icon icon="ep:refresh" />
          重置
        </el-button>
        <el-button @click="handleClickCreate"> 新建 </el-button>
      </el-form-item>
    </el-form>
    <Table
      :columns="columns"
      :page-param="queryParams"
      :adaptive="true"
      :page-data="queryData"
      save-key="Server"
      height-per="90"
      @page-change="getPage"
      @row-dblclick="handleTableDblclick"
    >
      <template #menu="{ row }">
        <context-menu-item label="修改" @click="handleClickUpdate(row)" />
        <context-menu-item label="删除" @click="handleClickDelete(row)" />
        <context-menu-item label="初始化" @click="handleClickInit(row)" />
      </template>
    </Table>
  </ContentWrap>
  <ServerEdit ref="serverEditRef" @success="getPage" />
  <ExecResultTip ref="resultTipRef" />
</template>
<script setup name="Server" lang="ts">
import ServerEdit from './ServerEdit.vue'
import * as DevopsServerApi from '@/api/infra/devops/server'
import ExecResultTip from '@/views/infra/devops/ExecResultTip.vue'
const serverEditRef = ref()
const resultTipRef = ref()
const queryFormRef = ref()
const queryParams = ref({
  ip: undefined
})
const queryData = ref()
const columns = [
  {
    label: '服务器名称',
    prop: 'name'
  },
  {
    label: '服务器IP',
    prop: 'ip'
  },
  {
    label: '域名',
    prop: 'domainName'
  },
  {
    label: '内网IP',
    prop: 'internalIp'
  },
  {
    label: '服务器密码',
    prop: 'serverPassword'
  },
  {
    label: '数据库密码',
    prop: 'databasePassword'
  },
  {
    label: '后端服务端口',
    prop: 'port'
  },
  {
    label: '前端服务端口',
    prop: 'frontPort'
  },
  {
    label: '唯一码',
    prop: 'uniqueCode'
  }
]
/** 初始化 */
onMounted(() => {
  getPage()
})

/** 搜索按钮操作 */
const handleClickSearch = () => {
  getPage()
}

/** 重置按钮操作 */
const handleClickReset = () => {
  queryFormRef.value?.resetFields()
  handleClickSearch()
}

/** 新建按钮操作 */
const handleClickCreate = () => {
  serverEditRef.value.open('create', 0)
}

/** 处理响应事件  */
const getPage = async () => {
  queryData.value = await DevopsServerApi.pageQuery(queryParams.value)
}

/** 处理table行双击事件 */
const handleTableDblclick = (row) => {
  serverEditRef.value.open('update', row.id)
}

/** 处理右击事件 修改 */
const handleClickUpdate = (row) => {
  serverEditRef.value.open('update', row.id)
}

/** 处理右击事件 删除 */
const handleClickDelete = async (row) => {
  await DevopsServerApi.deleted(row.id)
  getPage()
}

const handleClickInit = async (row) => {
  resultTipRef.value.open(row.id, 'server-init')
}
</script>
